package com.dykj.dati.controller.h5;


import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dykj.dati.config.RedisCache;
import com.dykj.dati.model.t.TJiaojuan;
import com.dykj.dati.model.user.HUser;
import com.dykj.dati.service.t.ITJiaojuanService;
import com.dykj.dati.service.user.IHUserService;
import com.dykj.dati.utils.ReturnUtil;
import com.dykj.dati.utils.ServiceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

import static com.dykj.dati.utils.ServiceUtil.*;

/**
 * @Author: yc
 * @Date: 2021/9/22 15:48
 */
@Controller
@RequestMapping("/dth5")
public class H5LoginController {

    @Autowired
    private IHUserService userService;
    /**
     * 跳转登录页
     * @return
     */
    @GetMapping("/login")
    public String loginIndex() {
        return "h5/login/index";
    }

    /**
     * 跳转答题类型页
     * @return
     */
    @GetMapping("/intype")
    public String jumpIntype(){
        return "h5/login/intype";
    }

    /**
     *
     * @param type
     * @return
     */
    @GetMapping("/jumpPage")
    public String jumpPage(Model model,String type,String uid){
        model.addAttribute("type",type);
         if("1".equals(type)){
            String tz=isJswb( uid)?"h5/login/intype":"h5/ti/contest";
            model.addAttribute("msg",isJswb(uid)?"0":"1");
            return tz;
        }
        return "h5/ti/practice";
    }

    /**
     * 跳转我的答题列表
     * @param userId
     * @return
     */
    @GetMapping("/jumpWoDeDt")
    public String jumpWoDeDt(Model model,String userId){
        model.addAttribute("list",  getJlByUid(userId));

        return "h5/ti/answerlist";
    }

    /**
     *跳转我的答题详细
     * @param cId
     * @return
     */
    @GetMapping("/jumpDtXiangXi")
    public String jumpDtXiangXi(Model model,String cId){

        model.addAttribute("xq",getXiangQingByCid(cId));
        return "h5/ti/answerlistresult";
    }
    /**
     * 用户登录
     * @param response
     * @param request
     * @param userName
     * @param phone
     * @return
     */
    @PostMapping("/userLogin")
    @ResponseBody
    public ModelMap userLogin(HttpServletResponse response, HttpServletRequest request,
                              @RequestParam(value = "username", required = true) String userName,
                              @RequestParam(value = "phone", required = true) String phone) {
        //验证手机号
        long count= ServiceUtil.getUserCountByPhone(phone);
        if(count==0){
            String id=IdUtil.simpleUUID();
            HUser user=new HUser();
            user.setCreateTime(new Date());
            user.setId(id);
            user.setName(userName);
            user.setPhone(phone);
            userService.save(user);
            return ReturnUtil.Success("登录成功",user.getId());
        }else{
            HUser hUser = getUserByPhone(phone);
            return ReturnUtil.Success("登录成功",hUser.getId());
        }
    }
}
